/*
 * @Descripttion: 
 * @version: 
 * @Author: weni09
 * @Date: 2021-04-19 10:44:07
 * @LastEditors: weni09
 * @LastEditTime: 2021-06-15 10:17:26
 * @FilePath: \icoder-frontend\blog\src\router\index.js
 */

import Vue from "vue";
import Router from "vue-router";
import store from "@/store";
Vue.use(Router);

const routes = [
{
	path:'/',
    // component:(home)
    redirect:'/home',
},
{
	path:'/home',
	name:'home',
    component: () => import("../views/Home" )
},

//分类/标签列表路由
{
    path: '/articles/:res/:slug',
    name: 'res', 
    component: () => import('../views/Home.vue'),
},

{
    path:'/artDetail/:id',
    name:'artDetail',
    component: () => import("../views/ArticleDetail")
},
{
    path:'/about',
    name:'about',
    component: () => import("../views/About")
},
{
    path:'/archive',
    name:'archive',
    component: () => import("../views/Archive")
},

{
    path:'/article/edit/:id?',
    name:'articleEdit',
    meta:{requireAuth: true},
    component: () => import("../views/ArticleEdit")
},


{
    path:'/about/edit/:id?',
    name:'aboutEdit',
    meta:{requireAuth: true},
    component: () => import("../views/AboutEdit")
},


{
    path:'*',
    redirect:'/'
}
];




const router = new Router({
    mode: 'history',
    scrollBehavior: () => ({ y: 0 }),
    routes,
});

router.beforeEach((to,from,next)=>{
    if(to.meta.requireAuth){
        //需要权限的路由，检测token有放行
        if (store.getters.auth.token){
          next() //放行
        }
        else{
           next({path:"/"})
        }
       }
      else{
          next()
      }
})

export default router;
